"use strict";
/* Controllers */
app.controller("zhibiaoController", ['$scope', '$location', '$rootScope', '$routeParams', 'zhibiaoService', function ($scope, $location, $rootScope, $routeParams, zhibiaoService) {

    var vm = $scope.vm = {};
    vm.result = {};
    vm.p = {};
    vm.p.id = $routeParams.id; //
    vm.p.nodeId=$routeParams.nodeId;

    vm.p.url = "";
    vm.p.dashboardType = 'zhili';


    var errorFn = function (result) {
        alert(result);
    };

    $scope.loadUrl = function () {
        var vm = $scope.vm;
        vm.result.ms = "...";

        var param = {
            dashboardType: vm.p.dashboardType
        };
        var procFn = function (response) {
            var data = response.data;
            if (data == null) {
                return;
            }
            // 匹配节点
            var findText = function(arr, text) {
                var idx = -1;
                for(var i=0;i<arr.length;++i) {
                    if(arr[i].text==text) {
                        idx = i;
                        break;
                    }
                }
                return idx;
            };
            var addNode = function(tree, biz, group, node) {
                var bizNode;
                var bizIdx = findText(tree, biz);
                if(bizIdx==-1) {
                    bizNode = {text: biz, nodes:[]};
                    tree.push(bizNode);
                }else {
                    bizNode = tree[bizIdx];
                }

                var groupNode;
                var groupIdx = findText(bizNode.nodes, group);
                if(groupIdx==-1) {
                    groupNode = {id: group, text: group, nodes:[]};
                    bizNode.nodes.push(groupNode);
                }else {
                    groupNode = bizNode.nodes[groupIdx];
                }
                groupNode.nodes.push(node);
            };
            var treeData = function(data) {
                var tree =[];
                for(var i=0; i<data.length; ++i) {
                    var dashboard = {
                        id: data[i].id,
                        text: data[i].dashboardName,
                        frame_href: data[i].dashboardHref
                    };
                    if(data[i].id==vm.p.nodeId) {
                        vm.p.nodeIdParent = data[i].groupName;
                    }
                    addNode(tree,data[i].bizName, data[i].groupName, dashboard);
                }
                return tree;
            };

            $('#tree').treeview({
                data: treeData(data),
                expanded: false,//初始是否展开
                levels: 2,//初始显示层数
                onNodeSelected: function (event, data) {
                    if(data.frame_href==null) return;
                    $("#preview").attr("href",data.frame_href)
                    $('#iframe_page').attr("src",data.frame_href); // 设置iframe内容

                    // 修改浏览器url
                    history.pushState(
                        {id:data.id},null,"#/star/metric2/zhibiao/0/"+data.id
                    );
                }
            });

            // 初始化
            if(typeof(vm.p.nodeId)!="undefined"&&vm.p.nodeId!=null&&vm.p.nodeId!=0){
                var parentId=$("[data-node-id="+vm.p.nodeIdParent+"]").attr("data-nodeid")
                if(typeof(parentId)!="undefined") {
                    $('#tree').treeview('expandNode', [ Number(parentId), { levels:2, silent:true}]);
                    var nodeid=$("[data-node-id="+vm.p.nodeId+"]").attr("data-nodeid")
                    if(typeof(nodeid)!="undefined") {
                        $('#tree').treeview('selectNode', [ Number(nodeid), { silent: false } ]);
                    }
                }
            }
        };
        return zhibiaoService.loadDashboard(param, procFn, errorFn);
    };


    $scope.initializeController = function () {
        var vm = $scope.vm;

        $scope.loadUrl();
    };

    $scope.initializeController();
}]);


/* Services */
app.service('zhibiaoService', ['ajaxService', function (ajaxService) {
    this.loadDashboard = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/dashboard/loadDashboard.do", successFunction, errorFunction);
    };
}]);


